package com.ztstech.ali_vod_video.alivideo.utils;

import com.ztstech.ali_vod_video.alivideo.base.BaseCallBack;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Dispatcher;
import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okio.BufferedSink;

public class OkHttpUtil {

    private static OkHttpClient sHttpClient;

    static {
        Dispatcher dispatcher = new Dispatcher();
        dispatcher.setMaxRequests(100);
        dispatcher.setMaxRequestsPerHost(30);
        sHttpClient = new OkHttpClient.Builder()
                .connectTimeout(15, TimeUnit.SECONDS)
                .writeTimeout(15, TimeUnit.SECONDS)
                .readTimeout(15, TimeUnit.SECONDS)
                .build();
    }

    /**
     * post请求
     *
     * @param url
     * @param map
     */
    public static void requestNet(String url, Map<String, String> map, final BaseCallBack<String> callBack) {
        HttpUrl.Builder urlBuilder = HttpUrl.parse(url).newBuilder();
        if (map != null) {
            for (String key : map.keySet()) {
                if (map.get(key) != null) {
                    urlBuilder.setQueryParameter(key, map.get(key));
                    LogUtil.e("参数："+key+"："+map.get(key));
                }
            }
        }
        final Request request = new Request.Builder()
                .get()
                .url(urlBuilder.build())
                .build();
        Call call = sHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                callBack.onFail(e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) {
                if (response == null || response.body() == null) {
                    callBack.onFail("数据错误");
                } else {
                    try {
                        String rawData = response.body().string();
                        callBack.onSuccess(rawData);
                    } catch (IOException e) {
                        e.printStackTrace();
                        callBack.onFail("数据错误");
                    }
                }
            }
        });
    }

    /**
     * post请求
     *
     * @param url
     * @param rawJson
     */
    public static <T> void postRawJson(String url, String rawJson, final BaseCallBack<String> callBack) {
        MediaType mediaType = MediaType.parse("application/json");
        Request request = new Request.Builder()
                .url(url)
                .post(RequestBody.create(mediaType, rawJson))
                .build();
        Call call = sHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                callBack.onFail(e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) {
                if (response == null || response.body() == null) {
                    callBack.onFail("数据错误");
                } else {
                    try {
                        String rawData = response.body().string();
                        callBack.onSuccess(rawData);
                    } catch (IOException e) {
                        e.printStackTrace();
                        callBack.onFail("数据错误");
                    }
                }
            }
        });
    }


}
